﻿Imports System.Data.SqlClient

Public Class NewZR

    Private user As String
    Private userSite As String
    Private cn As CnData
    Private da As SqlDataAdapter
    Private ds As DataSet
    Private cmd As SqlCommand
    Private dtr As SqlDataReader
    Private choix As String

    Public Sub New(ByVal user As String, ByRef cn As CnData, ByVal userSite As String)
        ' Cet appel est requis par le concepteur.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        Me.user = user
        Me.cn = cn
        Me.userSite = userSite

    End Sub

    Private Sub NewZR_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: cette ligne de code charge les données dans la table 'LaisserPasserDS.ZR'. Vous pouvez la déplacer ou la supprimer selon vos besoins.
        cn.Open()
        da = New SqlDataAdapter("SELECT Distinct TypeFlux FROM ZR WHERE Actif = 1 ORDER BY TypeFlux", cn.getCn)
        ds = New DataSet
        da.Fill(ds, "Type")
        With TypeF
            .DataSource = ds.Tables("Type")
            .DisplayMember = "TypeFlux"
            .ValueMember = "TypeFlux"
        End With

        da = New SqlDataAdapter("SELECT '00' as NumSite, 'Tous' as NomDoc FROM Sites UNION SELECT NumSite,NomDoc FROM Sites WHERE Actif = 1", cn.getCn)
        ds = New DataSet
        da.Fill(ds, "Sites")
        With SiteZR
            .DataSource = ds.Tables("Sites")
            .DisplayMember = "NomDoc"
            .ValueMember = "NumSite"
            .SelectedValue = "00"
        End With

        cmd = New SqlCommand("SELECT * from ZR WHERE TypeFlux='" & TypeF.SelectedValue & "' AND Actif = 1", cn.getCn)
        'val = cmd.ExecuteScalar
        dtr = cmd.ExecuteReader()
        dtr.Read()

        CR.Text = dtr(dtr("Choix").ToString).ToString
        choix = dtr("Choix").ToString
        If String.IsNullOrEmpty(dtr("INFO2").ToString) Then
            Panel2.Visible = False
        Else
            Panel2.Visible = True
            VL2.Text = dtr("INFO2").ToString
        End If
        VL1.Text = dtr("INFO1").ToString
        'Rayon.Text = dtr("Rayon").ToString
        'ZR.Text = dtr("ZR").ToString()

        cn.Close()

    End Sub

    Private Sub TypeF_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TypeF.SelectedIndexChanged
        If TypeF.ValueMember = "" Then
            Exit Sub
        End If
        If TypeF.DisplayMember = "" Then
            Exit Sub
        End If
        If String.IsNullOrEmpty(TypeF.Text) Then
            Exit Sub
        End If


        cn.Open()
        cmd = New SqlCommand("SELECT * from ZR WHERE TypeFlux='" & TypeF.SelectedValue & "' AND Actif = 1", cn.getCn)
        'val = cmd.ExecuteScalar
        dtr = cmd.ExecuteReader()
        dtr.Read()

        CR.Text = dtr(dtr("Choix").ToString).ToString
        choix = dtr("Choix").ToString
        If String.IsNullOrEmpty(dtr("INFO2").ToString) Then
            Panel2.Visible = False
            VL2.Text = ""
            VV2.Text = ""
            VVC2.Text = ""
        Else
            Panel2.Visible = True
            VL2.Text = dtr("INFO2").ToString
        End If
        VL1.Text = dtr("INFO1").ToString
        'Rayon.Text = dtr("Rayon").ToString
        'ZR.Text = dtr("ZR").ToString()
        dtr.Close()
        cn.Close()
    End Sub


    Private Sub Fermer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Fermer.Click
        Me.Close()
    End Sub

    Private Sub Enregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Enregistrer.Click
        If String.IsNullOrEmpty(Rayon.Text) Then
            MsgBox("Veuillez saisir le rayon")
            Exit Sub
        ElseIf String.IsNullOrEmpty(ZR.Text) Then
            MsgBox("Veuillez saisir la zone de roulage")
            Exit Sub
        ElseIf choix = "INFO1" And (String.IsNullOrEmpty(VV1.Text) Or String.IsNullOrEmpty(VVC1.Text)) Then
            MsgBox("Veuillez saisir la valeurs 1")
            Exit Sub
        ElseIf choix = "INFO2" And (String.IsNullOrEmpty(VV2.Text) Or String.IsNullOrEmpty(VVC2.Text)) Then
            MsgBox("Veuillez saisir la valeurs 2")
            Exit Sub
        End If

        If choix = "Rayon" Then
            cn.Open()
            If SiteZR.SelectedValue = "00" Then
                cmd = New SqlCommand("SELECT * from ZR WHERE TypeFlux='" & TypeF.SelectedValue & "' AND Actif = 1 AND Rayon='" & Rayon.Text & "'", cn.getCn)
            Else
                cmd = New SqlCommand("SELECT * from ZR WHERE TypeFlux='" & TypeF.SelectedValue & "' AND Site = '" & SiteZR.SelectedValue & "' AND Actif = 1 AND Rayon='" & Rayon.Text & "'", cn.getCn)
            End If

            dtr = cmd.ExecuteReader()
            If dtr.HasRows Then
                MsgBox("Ce Rayon existe déjà")
                dtr.Close()
                Exit Sub
            End If
            dtr.Close()
            cn.Close()
        ElseIf choix = "INFO1" Then
            cn.Open()
            If SiteZR.SelectedValue = "00" Then
                cmd = New SqlCommand("SELECT * from ZR WHERE TypeFlux='" & TypeF.SelectedValue & "' AND Actif = 1 AND INFO1_Doc='" & VV1.Text & "'", cn.getCn)
            Else
                cmd = New SqlCommand("SELECT * from ZR WHERE TypeFlux='" & TypeF.SelectedValue & "' AND Site = '" & SiteZR.SelectedValue & "' AND Actif = 1 AND INFO1_Doc='" & VV1.Text & "'", cn.getCn)
            End If

            dtr = cmd.ExecuteReader()
            If dtr.HasRows Then
                MsgBox("La valeur 1 existe déjà")
                dtr.Close()
                Exit Sub
            End If
            dtr.Close()
            cn.Close()
        ElseIf choix = "INFO2" Then
            cn.Open()
            If SiteZR.SelectedValue = "00" Then
                cmd = New SqlCommand("SELECT * from ZR WHERE TypeFlux='" & TypeF.SelectedValue & "' AND Actif = 1 AND INFO2_Doc='" & VV2.Text & "'", cn.getCn)
            Else
                cmd = New SqlCommand("SELECT * from ZR WHERE TypeFlux='" & TypeF.SelectedValue & "' AND Site = '" & SiteZR.SelectedValue & "' AND Actif = 1 AND INFO2_Doc='" & VV2.Text & "'", cn.getCn)
            End If

            dtr = cmd.ExecuteReader()
            If dtr.HasRows Then
                MsgBox("La valeur 2 existe déjà")
                dtr.Close()
                Exit Sub
            End If
            dtr.Close()
            cn.Close()
        End If
        'dtr.Close()
        cn.Open()
        If SiteZR.SelectedValue = "00" Then
            cmd = New SqlCommand("Insert into zr (TypeFlux,ZR,Rayon,INFO1,INFO1_Eti,INFO1_Doc,INFO2,INFO2_Eti,INFO2_Doc,Choix,Util,DateA,HeureA,Actif) " _
            & " values ('" & TypeF.SelectedValue & "','" & ZR.Text & "','" & Rayon.Text & "','" & VL1.Text & "','" & VVC1.Text & "','" & VV1.Text & "','" & VL2.Text & "','" & VVC2.Text & "','" & VV2.Text & "','" & choix & "','" & user & "','" & Date.Today & "','" & Date.Now.ToLongTimeString & "','true')", cn.getCn)
        Else
            cmd = New SqlCommand("Insert into zr (TypeFlux,ZR,Rayon,INFO1,INFO1_Eti,INFO1_Doc,INFO2,INFO2_Eti,INFO2_Doc,Choix,Site,Util,DateA,HeureA,Actif) " _
            & " values ('" & TypeF.SelectedValue & "','" & ZR.Text & "','" & Rayon.Text & "','" & VL1.Text & "','" & VVC1.Text & "','" & VV1.Text & "','" & VL2.Text & "','" & VVC2.Text & "','" & VV2.Text & "','" & choix & "'," & SiteZR.SelectedValue & ",'" & user & "','" & Date.Today & "','" & Date.Now.ToLongTimeString & "','true')", cn.getCn)
        End If

        cmd.ExecuteNonQuery()
        MsgBox("Zone de roulage enregistrée")
        cn.Close()
        Me.Close()
    End Sub
End Class